<?php
defined('_JEXEC') or die( 'Restricted access' );
jimport( 'joomla.application.component.controller' );
jimport('joomla.filesystem.file');
class DanhMucController extends JController
{
	function __construct($config = array()){
		parent::__construct($config);
		$this->registerTask( 'add',  'display' );
		$this->registerTask( 'edit', 'display' );
		$this->registerTask( 'publish', 'published' );
		$this->registerTask( 'unpublish', 'unpublished' );
	}
	function display(){
		switch($this->getTask()){
			case 'add'     :
				JRequest::setVar( 'hidemainmenu', 0 );
				JRequest::setVar( 'layout', 'default'  );
				JRequest::setVar( 'view'  , 'detail');
				JRequest::setVar( 'edit', false );
				break;
			case 'edit':
				JRequest::setVar( 'hidemainmenu', 0 );
				JRequest::setVar( 'layout', 'default'  );
				JRequest::setVar( 'view'  , 'detail');
				JRequest::setVar( 'edit', true );
				break;
			case 'homepage':
				JRequest::setVar('view','homepage');
				JRequest::setVar('layout','default');
				break;
			default:			
				JRequest::setVar('view','list');
				JRequest::setVar('layout','default');
				break;
		}
		parent::display();
	}
	function save(){		
		global $mainframe;
		$post	= JRequest::get('post');		
		$db = &JFactory::getDBO();
		$date = JFactory::getDate();
		if($post['createday']=='')
			$post['createday'] = $date->toMySQL();
		$cid	= JRequest::getVar( 'cid', array(0), 'post', 'array' );
		$post['id'] = (int) $cid[0];		
		
		$model = $this->getModel('detail');
		$sql = "Select count(*) from #__product where parent =".$post['id'];
		$db->setQuery($sql);
		$row = $db->loadResult();
		
		if ($model->store($post)) {
			$msg = JText::_( 'Lưu thành công' );
		} else {
			$msg = JText::_( 'Có lỗi' );
		}
		$link = 'index.php?option=com_danhmuc';
		$this->setRedirect($link, $msg);
	}
	function published(){
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Bạn chưa chọn gi hết' ) );
		}
		$model = $this->getModel(detail);
		if(!$model->publish($cid, 1)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_danhmuc' );
	}
	function unpublished(){
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to publish' ) );
		}
		$model = $this->getModel(detail);
		if(!$model->publish($cid, 0)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_danhmuc' );
	}
	function publishfrontpage(){
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Bạn chưa chọn gi hết' ) );
		}
		$model = $this->getModel(detail);
		if(!$model->publishfrontpage($cid, 1)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_danhmuc','Bật danh mục ra trang chủ thành công');
	}
	function unpublishfrontpage(){
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to publish' ) );
		}
		$model = $this->getModel(detail);
		if(!$model->publishfrontpage($cid, 0)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_danhmuc','Tắt danh mục trang chủ thành công');
	}
	function remove(){
		global $mainframe,$option;
		$db = &JFactory::getDBO();
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Bạn chưa chọn gi hết' ) );
		}
		$model = $this->getModel(detail);		
		if(!$model->delete($cid)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_danhmuc','Đã xóa' );
	}

	function orderup(){
		$model = $this->getModel(detail);
		$model->move(-1);
		$this->setRedirect( 'index.php?option=com_danhmuc');
	}
	function orderdown()
	{
		$model = $this->getModel(detail);
		$model->move(1);
		$this->setRedirect( 'index.php?option=com_danhmuc');
	}
	function saveorder(){
		$cid 	= JRequest::getVar( 'cid', array(), 'post', 'array' );
		$order 	= JRequest::getVar( 'ordering', array(), 'post', 'array' );
		
		JArrayHelper::toInteger($cid);
		JArrayHelper::toInteger($order);
		
		if(strpos($_SERVER['HTTP_REFERER'],'task=')){
			$model = $this->getModel(detail);
			$model->saveorderfrontpage($cid, $order);
	
			$msg = JText::_( 'New ordering saved' );
			$this->setRedirect( 'index.php?option=com_danhmuc&task=homepage', $msg );	
		}else{ // normal
			$model = $this->getModel(detail);
			$model->saveorder($cid, $order);
	
			$msg = JText::_( 'New ordering saved' );
			$this->setRedirect( 'index.php?option=com_danhmuc', $msg );
		}
		

		
	}
}